package com.sunnada.edu.system.sysmodule.controller;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sunnada.edu.system.pub.common.BasicController;
import com.sunnada.edu.system.pub.common.Tree;
import com.sunnada.edu.system.pub.common.TreeMethods;
import com.sunnada.edu.system.pub.dao.Page;
import com.sunnada.edu.system.pub.filter.XssHttpServletRequestWrapper;
import com.sunnada.edu.system.pub.service.PubMethods;
import com.sunnada.edu.system.pub.util.Utils;
import com.sunnada.edu.system.sysmodule.bean.Module;
import com.sunnada.edu.system.sysmodule.service.ModuleService;

/***
 * explain 模块控制类 
 * @author chs 2015-5-12
 * version 1.0
 */
@Controller
@RequestMapping("/moduleController")
public class ModuleController extends BasicController{
	@Resource
	ModuleService moduleService;
	/**模块首页*/
	@RequestMapping("/module")
	public String module(HttpServletRequest request) { 
		String moduleId=request.getParameter("moduleId");
		request.setAttribute("pathValue", PubMethods.getPathValue(moduleId));
		return "system/sysmodule/module";
	}
	/**模块查询*/
	@RequestMapping("/moduleSearch")
	public void moduleSearch(HttpServletRequest request,HttpServletResponse response) { 
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, Module.class, params);
		Page page = new Page(); 
		page.setRowCount(moduleService.getModuleCots(params));
		page.setPageMap(request, page, params);
		List<?> list = moduleService.findModules(params);
		JSONObject json = new JSONObject();		
		json.put("list", list);
		json.put("params", params); 
		printInfo(response,json);
	}
	/**模块添加页面*/
	@RequestMapping("/moduleAdd")
	public String moduleAdd(HttpServletRequest request) { 
		return "system/sysmodule/module_add";
	}
	/**模块修改页面*/
	@RequestMapping("/moduleUpdate")
	public String moduleUpdate(HttpServletRequest request) { 
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, Module.class, params);
		Map module = moduleService.getModuleByRecId(params);
		request.setAttribute("module", module);
		return "system/sysmodule/module_update";
	}
	/**模块保存功能 */
	@RequestMapping("/moduleSave")
	public void moduleSave(HttpServletRequest request,HttpServletResponse response) { 
		HashMap<String, String> params = new HashMap<String, String>();
		HttpServletRequest re=XssHttpServletRequestWrapper.getOrgRequest(request);
		setReqParams(re, Module.class, params);
		String recId = params.get("recId");
		if (Utils.isNull(recId)) {
			msg = moduleService.moduleAdd(params);
		}else{
			msg = moduleService.moduleUpdate(params);
		}
		printInfo(response,msg);
	}
	/**模块删除功能*/
	@RequestMapping("/moduleDelete")
	public void moduleDelete(HttpServletRequest request,HttpServletResponse response) { 
		HashMap<String, String> params = new HashMap<String, String>();
		String recIds = request.getParameter("recIds");
		params.put("recIds",Utils.getSqlSplitStr(recIds));
		msg = moduleService.moduleDel(params);		
		printInfo(response,msg);
	}
	/**模块树*/
	@RequestMapping("/moduleTree")
	public void moduleTree(HttpServletResponse response,HttpServletRequest request) {
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, Module.class, params);
		List list=moduleService.moduleTree(params);
		List treeList = new ArrayList<Tree>();
		treeList = TreeMethods.setTreePropertyForModule(list,true);
		printInfo(response,JSONArray.toJSONString(treeList).toString());
	}
	/**模块树：全展开 ,同步加载 */
	@RequestMapping("/moduleTree2")
	public void moduleTree2(HttpServletResponse response,HttpServletRequest request) {
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, Module.class, params);
		List list=moduleService.moduleTree2(params);
		JSONObject map = new JSONObject();
		map.put("list", list);
		printInfo(response,map);
	}
	
	/**系统模块树模块树：全展开 ,同步加载 */
	@RequestMapping("/moduleTree3")
	public void moduleTree3(HttpServletResponse response,HttpServletRequest request) {
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, Module.class, params);
		List list=moduleService.moduleTree3(params);
		JSONObject map = new JSONObject();
		map.put("list", list);
		printInfo(response,map);
	}
	
	/**模块启用功能 zjc 2015-5-26*/
	@RequestMapping("/moduleEnable")
	public void moduleEnable(HttpServletRequest request,HttpServletResponse response){
		HashMap<String, String>params=new   HashMap<String, String>();
		String recIds=request.getParameter("recIds");
		params.put("recIds",Utils.getSqlSplitStr(recIds));
		msg=moduleService.moduleEnable(params);
		printInfo(response, msg);
	}
	
	/**模块停用功能 zjc 2015-5-26*/
	@RequestMapping("/moduleDisable")
	public  void moduleDisable(HttpServletRequest request,HttpServletResponse response){
		HashMap<String, String>params=new HashMap<String, String>();
		String recIds=request.getParameter("recIds");
		params.put("recIds",Utils.getSqlSplitStr(recIds));
		msg=moduleService.moduleDisable(params);
		printInfo(response, msg);
	}
}
